package com.elitect.ftptransfer.module.main.handler;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.elitect.ftptransfer.R;
import com.elitect.ftptransfer.dialog.InputTextDialog;
import com.elitect.ftptransfer.dialog.InputTextDialog.InputListener;
import com.elitect.ftptransfer.module.main.data.FileItem;
import com.elitect.ftptransfer.module.main.ui.ListFilesAdapter;

/**
 * Class to handle the file browser.
 * @author NgocTan
 *
 */
public class FileBrowserHandler implements OnClickListener, InputListener
{
    private static final int INDEX_ICON_NEW_FOLDER = 0;
    private static final int INDEX_ICON_FOLDER = INDEX_ICON_NEW_FOLDER + 1;
    private static final int INDEX_ICON_FILE = INDEX_ICON_FOLDER + 1;
    private static final int INDEX_ICON_BACK = INDEX_ICON_FILE + 1;
    private static final int INDEX_ICON_ACTION = INDEX_ICON_BACK + 1;
    private static final int INDEX_ICON_PATH = INDEX_ICON_ACTION + 1;
    private static final int INDEX_COLOR_PATH = INDEX_ICON_PATH + 1;

    private int[] mResources;
    private Context mContext;
    private String mRootPath;

    private View mView;

    private TextView mTextPath;
    private ListView mListViewFile;
    private ImageButton mButtonNewFolder;
    private ImageButton mButtonAction;
    private ImageButton mButtonBack;

    private ListFilesAdapter mListFilesAdapter;
    private OnLoadFilesHandler mOnHandleLoadFiles;
    private OnFileActionHandler mOnFileActionHandler;
    
    private InputTextDialog mInputTextDialog;

    public FileBrowserHandler(Context context, int[] resources,
            ViewStub viewStub, OnLoadFilesHandler onHandleLoadFiles, OnFileActionHandler onFileActionHandler)
    {
        mContext = context;
        mResources = resources;
        mView = viewStub.inflate();
        mOnHandleLoadFiles = onHandleLoadFiles;
        mOnFileActionHandler = onFileActionHandler;

        mTextPath = (TextView) mView.findViewById(R.id.path);
        mListViewFile = (ListView) mView.findViewById(R.id.list_files);
        mButtonAction = (ImageButton) mView.findViewById(R.id.action);
        mButtonBack = (ImageButton) mView.findViewById(R.id.back);
        mButtonNewFolder = (ImageButton) mView.findViewById(R.id.new_folder);

        mTextPath.setCompoundDrawablesWithIntrinsicBounds(
                mContext.getResources().getDrawable(mResources[INDEX_ICON_PATH]),
                null, null, null);
        mTextPath.setBackgroundColor(resources[INDEX_COLOR_PATH]);

        mButtonAction.setImageResource(mResources[INDEX_ICON_ACTION]);
        mButtonBack.setImageResource(mResources[INDEX_ICON_BACK]);
        mButtonNewFolder.setImageResource(mResources[INDEX_ICON_NEW_FOLDER]);
        
        mButtonAction.setOnClickListener(this);
        mButtonBack.setOnClickListener(this);
        mButtonNewFolder.setOnClickListener(this);
        
        mListViewFile.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                FileItem fileItem = (FileItem) parent.getItemAtPosition(position);
                if (fileItem == null)
                {
                    return;
                }
                if (fileItem.isFile())
                {
                    // TODO with file
                }
                else
                {
                    mOnHandleLoadFiles.onLoadFiles(mTextPath.getText().toString()
                            + fileItem.getName() + "/");
                }
            }
        });
        
        mInputTextDialog = new InputTextDialog(context, mContext.getResources().getString(R.string.new_folder), this);
    }
    
    public void start(String rootPath)
    {
        mRootPath = rootPath;
        mOnHandleLoadFiles.onLoadFiles(rootPath);
    }

    public void refreshView(String path, List<FileItem> fileItems)
    {
        mListFilesAdapter = new ListFilesAdapter(mContext, fileItems,
                mResources[INDEX_ICON_FILE], mResources[INDEX_ICON_FOLDER]);
        mListViewFile.setAdapter(mListFilesAdapter);
        
        if(path.equals(mRootPath))
        {
            mButtonBack.setVisibility(View.INVISIBLE);
        }
        else
        {
            mButtonBack.setVisibility(View.VISIBLE);
        }
        mTextPath.setText(path);
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.action)
        {
            mOnFileActionHandler.onFileAction(mListFilesAdapter.getDatas());
        }
        else if (v.getId() == R.id.back)
        {
            String path = mTextPath.getText().toString();
            String[] strings = path.split("[/]");
            if(strings.length > 0)
            {
                String parentsPath = path.substring(0, path.length() - strings[strings.length - 1].length() - 1);
                mOnHandleLoadFiles.onLoadFiles(parentsPath);
            }
        }
        else if (v.getId() == R.id.new_folder)
        {
            mInputTextDialog.show();
        }
    }
    
    public String getCurrentPath()
    {
        return mTextPath.getText().toString();
    }

    @Override
    public void onInputText(String text)
    {
        mOnFileActionHandler.onCreateNewFolder(text.trim());
    }

    public void unavailable()
    {
        mButtonAction.setVisibility(View.INVISIBLE);
        mButtonNewFolder.setVisibility(View.INVISIBLE);
        mButtonBack.setVisibility(View.INVISIBLE);
    }
}
